package com.flyco.modules.sms.service.impl;

import com.flyco.modules.sms.entity.SmsPromotion;
import com.flyco.modules.sms.entity.SmsPromotionGoodsRef;
import com.flyco.modules.sms.mapper.SmsPromotionGoodsRefMapper;
import com.flyco.modules.sms.mapper.SmsPromotionMapper;
import com.flyco.modules.sms.service.ISmsPromotionService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 活动表
 * @Author: flyco
 * @Date:   2022-05-05
 * @Version: V1.0
 */
@Service
public class SmsPromotionServiceImpl extends ServiceImpl<SmsPromotionMapper, SmsPromotion> implements ISmsPromotionService {

	@Autowired
	private SmsPromotionMapper smsPromotionMapper;
	@Autowired
	private SmsPromotionGoodsRefMapper smsPromotionGoodsRefMapper;
	
	@Override
	@Transactional
	public void saveMain(SmsPromotion smsPromotion, List<SmsPromotionGoodsRef> smsPromotionGoodsRefList) {
		smsPromotionMapper.insert(smsPromotion);
		for(SmsPromotionGoodsRef entity:smsPromotionGoodsRefList) {
			//外键设置
			entity.setPromotionId(smsPromotion.getId());
			smsPromotionGoodsRefMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(SmsPromotion smsPromotion,List<SmsPromotionGoodsRef> smsPromotionGoodsRefList) {
		smsPromotionMapper.updateById(smsPromotion);
		
		//1.先删除子表数据
		smsPromotionGoodsRefMapper.deleteByMainId(smsPromotion.getId());
		
		//2.子表数据重新插入
		for(SmsPromotionGoodsRef entity:smsPromotionGoodsRefList) {
			//外键设置
			entity.setPromotionId(smsPromotion.getId());
			smsPromotionGoodsRefMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(Long id) {
		smsPromotionGoodsRefMapper.deleteByMainId(id);
		smsPromotionMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			smsPromotionGoodsRefMapper.deleteByMainId(Long.valueOf(id.toString()));
			smsPromotionMapper.deleteById(id);
		}
	}
	
}
